Vue.filter('handleTime', (time)=>{
    return time1(time)
    function time1(t) {
        let s  = t.slice(0, 10)
        let year = Number(s.split('/')[0]) 
        let month = Number(s.split('/')[1])
        let day = Number(s.split('/')[2])
        //alert(year+" "+month+" "+day)
        let time1 = getDays(year, month, day) // 目标时间
        let time2 = getDays(vm.now.year, vm.now.month, vm.now.day) //参照时间
        if (year === vm.now.year) {//本年
            if (vm.now.year === year && vm.now.month === month && vm.now.day === day){ //当天
                return t.slice(11, 16) //只返回 时:分
            }else if (vm.now.year === year && time2 - time1 === 1) { //昨天(仅处理同年同月场景)
                return `昨天${t.slice(11, 16)}`  //返回 昨天+时:分
            }else if (vm.now.year === year && time2 - time1 > 1) {
                return t.slice(5, 16) //只返回 月/日 时:分
            }else{ //昨天以前
                return t.slice(5, 16) //只返回 月/日 时:分
            }
        }else{//往年
            return t.slice(0, 16) //返回完整时间 年/月/日 时:分
        } 
    }/**/
    function isfullyear(year){
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
            return true;
        }
    }
    function getDays(year, month, day){
        //把天数赋值给num
        var num = day;
        //如果用户输入1月 则直接返回天数
        if (month == 1) {
            return day;
        }
        var monthDay = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
        //循环遍历月份 循环条件是用户输入月份的前一个月份 
        for (var i = 0; i < month - 1; i++) {
            //num = 当月天数 + 之前每个月的天数
            num = num + monthDay[i]
        }
        //如果用户输入的年份是闰年并且输入的月份大于2 那么总天数加1
        if (isfullyear(year) && month > 2) {
            num++;
        }
        return num;
    }
})
var vm = new Vue({
    el: "section",
    data: {
        now: {},
        userid: "",
        userimg: "",
        userID: "",
        user_id: "",
        userIMG: "",
        note: "",
        messageList: [],
        contentList: [
        ],
        userMsg: "",
        
    },
    computed: {
        myStringByteCount: function() {  
            return this.getByteCount1(this.myString);  
        } 
    },
    methods: {
        getByteCount1: function(str) {  
            let count = 0;  
            for (let i = 0; i < str.length; i++) {  
                count += str.charCodeAt(i) > 127 ? 2 : 1;  
            }  
            return count;  
        },
        getByteCount(str) {  
            let count = 0;  
            for (let i = 0; i < str.length; i++) {  
                count += str.charCodeAt(i) > 127 ? 2 : 1;  
            }  
            return count;  
        },
        checkInput(){
            
        },
        send(){
            if(/^\s*$/.test(this.userMsg)){
                this.$toast({
                    message: `不能发送空白消息`,
                    forbidClick: true
                })
                this.userMsg = ""
            }else{
                //获取时间
                let D = new Date()
                let timestamp = +D
                let year = D.getFullYear()
                let month = D.getMonth()+1
                    month = (month < 10 ? '0' + month : month)
                let day = D.getDate()
                    day = (day < 10 ? '0' + day : day)
                let hour = D.getHours()
                    hour = (hour < 10 ? '0' + hour : hour)
                let minutes = D.getMinutes()
                    minutes = (minutes < 10 ? '0' + minutes : minutes)
                let seconds = D.getSeconds()
                    seconds = (seconds < 10 ? '0' + seconds : seconds)
                // 获取时间戳无需使用此方式utc+0时间戳是与utc+8时间戳一致的
                let time =  year+"/"+month+"/"+day+" "+hour+":"+minutes+":"+seconds
                this.messageList.push({
                    from: this.userid,
                    to: this.userID,
                    Content: this.userMsg,
                    time: time,
                    timestamp: timestamp
                })
                // document.querySelector('span.read').style.display='none'
                let time1 = setTimeout(()=>{
                    document.querySelector('.chatUl li:last-child .van-loading').style.display='flex'
                    document.querySelector('.chatUl li:last-child').scrollIntoView({
                        behavior: 'smooth'
                    })
                    document.querySelector('span.read').style.display='none'
                    this.userMsg = ""
                    clearTimeout(time1)

                }, 10)
                //发送请求
                // let userMsg = encodeURIComponent(this.userMsg)

                let url = `https://fc-mp-e8e2bde5-0c0c-4622-87b5-66f8b08c3856.next.bspapp.com/getArticleList1.0`
                let method = `sendMsg_8917Csv51dnygrF89vd71`
                let data = {
                    userid: this.userid,
                    userID: this.user_id,
                    content: this.userMsg
                }
                axios.post(`${url}/${method}`, data)
                .then(res=>{
                    if(res.data.code !== 0){
                        this.$toast({
                            message: `服务出错[${res.data.code}]`,
                            forbidClick: true
                        })
                    }else{
                        console.log(res.data)
                        document.querySelector('.chatUl li:last-child .van-loading').style.display='none'
                        document.querySelector('span.read').style.display='inline-block'
                    }
                })
                //处理完毕
  
            }
            
            
        },
        // isMultipleOfFiveMinutes(timestamp){  
        //     // 获取当前时间戳（毫秒）  
        //     const now = Date.now();  
              
        //     // 将传入的时间戳和当前时间戳转换为分钟数  
        //     const inputMinutes = Math.round(timestamp / (1000 * 60));  
        //     const currentMinutes = Math.round(now / (1000 * 60));  
              
        //     // 计算两者之间的分钟差  
        //     const diffMinutes = Math.abs(inputMinutes - currentMinutes);  
              
        //     // 判断分钟差是否是5的整数倍  
        //     return diffMinutes % 5 === 0;  
        // },
        isShowTime(time, index){
            let ord = index+1
            let isShowTime = false
            if(ord == this.messageList.length){
                isShowTime = true
            }else if(ord % 3 == 0){
                isShowTime = true
            }else if(ord == 1){
                isShowTime = true
            }
            return isShowTime
        },
        isShowRead(from, index){
            return (from == this.userid && index == this.messageList.length-1)
        },
    },
    mounted() {
        let date = new Date()
        let year = date.getFullYear()
        let month = date.getMonth()+1
        let day = date.getDate()
        this.now = {
            year: year,
            month: month,
            day: day
        }
        let params1 = location.search.substring(1)
        this.userID = params1.split('&')[0].split('=')[1]
        this.note = decodeURI(params1.split('&')[1].split('=')[1])
        // let hadNewMessage = (params1.split('&')[2].split('=')[1] === "true") ? true: false
        
        // 获取消息
        let userInfo = localStorage.getItem('XGB-user')
        this.userid = encodeURIComponent(JSON.parse(userInfo).id) 
        this.userimg = JSON.parse(userInfo).userImg
        // let userMsg = encodeURIComponent(this.userMsg)

        let url = `https://fc-mp-e8e2bde5-0c0c-4622-87b5-66f8b08c3856.next.bspapp.com/getArticleList1.0`
        let method = `getPrivateMessage_4C817cV14DB8A1RH4T7J`
        let data = {
            userid: this.userid,
            userID: this.userID,
            // hadNewMessage: hadNewMessage
        }
        console.log({
            userid: this.userid,
            userID: this.userID,
            note: this.note,
            data: data
        })
        axios.post(`${url}/${method}`, data)
        .then(res=>{
            if(res.data.code === 0){
                //接收到消息
                this.messageList = res.data.data.msgList
                this.userIMG = res.data.data.userImg
                this.user_id = res.data.data.user_id
                console.log(this.messageList)
                document.querySelector("header .title").innerText = `${this.note}`
                // messageList.forEach((item, index) => {
                //     senderList.forEach((item2, index2)=>{
                //         if(item.SenderID == item2._id){
                //             this.contentList.unshift({
                //                 role: (userid == item2._id) ? "me" : "others",
                //                 user: {
                //                     id: item2._id,
                //                     nickname: item2.name,
                //                     img: item2.userImg
                //                 },
                //                 msg: {
                //                     id: item._id,
                //                     content: item.Content,
                //                     time: item.time
                //                 }
                //             })
                //             return //终止内层循环
                //         }


                //     })
                // });
                setTimeout(()=>{
                    document.querySelector('.chatUl li:last-child').scrollIntoView({
                        behavior: 'smooth'
                    })
                    
                },1)


            }else{
                this.$toast({
                    message: `服务出错[${res.data.code}]`
                })
            }
        })
    }
})